<template>
  <div class="form-container">
    <el-form inline :model="form" ref="ruleForm" class="form-line" label-width="130">
      <el-form-item label="车辆编号/车牌号" prop="vehicleId">
        <el-input size="default" v-model="form.vehicleId" placeholder="车辆编号/车牌号"></el-input>
      </el-form-item>
      <el-form-item label="车辆类型" prop="vehicleType">
        <el-select v-model="form.vehicleType" placeholder="请选择车辆类型">
          <el-option label="1" value="1">1</el-option>
          <el-option label="2" value="2">2</el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div class="btn-container">
      <el-button type="primary" @click="handleReset(ruleForm as FormInstance)">重置</el-button>
      <el-button type="primary" @click="handleSearch">查询</el-button>
    </div>
  </div>
  <div class="table-container">
    <el-table
      border
      :cell-style="{ textAlign: 'center', fontSize: '13px' }"
      :header-cell-style="{
        textAlign: 'center',
        fontWeight: '100',
        fontSize: '13px',
        color: 'white',
        backgroundColor: '#009688'
      }"
      :data="tableData"
      style="width: 100%"
    >
      <el-table-column prop="vehicleId" label="车辆编号/车牌号">
        <template #default="scope"> {{ scope.row.vehicleId + "/" + scope.row.vehicleNumber }} </template>
      </el-table-column>
      <el-table-column prop="typeName" label="车辆类型" />
      <el-table-column prop="companyName" label="所属公司" />
      <el-table-column prop="driverLicenseTypeName" label="所属驾驶证类型" />
      <el-table-column prop="vehicleStatus" label="状态">
        <template #default="scope">
          <span v-if="scope.row.vehicleStatus == '1'">正常</span>
          <span v-else-if="scope.row.vehicleStatus == '2'">维修中</span>
          <span v-else-if="scope.row.vehicleStatus === '3'">报废</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="250">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="handleEdit(scope.row.id)"> 查看详情 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        size="small"
        v-model:current-page="form.pageNum"
        :page-sizes="[5, 10, 15, 20]"
        layout="prev, pager, next"
        @change="getCarList"
        :total="pageTotal"
        :page-size="form.pageSize"
      />
    </div>
  </div>
</template>

<script setup lang="tsx" name="useProTable">
import { ElMessage, FormInstance } from "element-plus";
import { ref, onMounted } from "vue";
import { useRouter } from "vue-router";
import { getCarListAPI } from "@/api/modules/city/carAPI";
import { CarManage } from "@/api/interface/city/car";
const router = useRouter();
//ProCompanyType 已经通过declare全局申明过不用再单独导入
let tableData = ref<CarManage.Row[]>([
  {
    id: 1,
    company: -2147483648,
    vehicleId: "string",
    vehicleIdentificationNumber: "string",
    vehicleNumber: "string",
    vehicleTypeName: -2147483648,
    vehicleStatus: "st",
    driverLicenseTypeName: "string",
    relateUserName: "string"
  },
  {
    id: 1,
    company: -2147483648,
    vehicleId: "string",
    vehicleIdentificationNumber: "string",
    vehicleNumber: "string",
    vehicleTypeName: -2147483648,
    vehicleStatus: "st",
    driverLicenseTypeName: "string",
    relateUserName: "string"
  },
  {
    id: 1,
    company: -2147483648,
    vehicleId: "string",
    vehicleIdentificationNumber: "string",
    vehicleNumber: "string",
    vehicleTypeName: -2147483648,
    vehicleStatus: "st",
    driverLicenseTypeName: "string",
    relateUserName: "string"
  }
]);
let pageTotal = ref<number>(0);
const form = ref<CarManage.SearchParams>({
  companyId: JSON.parse(localStorage.getItem("userInfo") || "null").companyId,
  pageNum: 1,
  pageSize: 10
});
const ruleForm = ref<FormInstance>();
const handleEdit = (id: string) => {
  router.push({
    path: "/cityLevel/cityCar/cityCarManage/detail",
    query: { id }
  });
};
const handleReset = (Elform: FormInstance) => {
  Elform.resetFields();
  getCarList();
};

const handleSearch = (): void => {
  form.value.pageNum = 1;
  getCarList();
};
// 列表接口请求地址
const getCarList = async () => {
  const { rows, total, code, msg } = await getCarListAPI(form.value);
  console.log(total, code);
  if (code == "200" && rows && total) {
    tableData.value = rows;
    pageTotal.value = total;
  } else {
    ElMessage.error(`获取列表失败,${msg}`);
  }
};
onMounted(() => {
  getCarList();
});
</script>
<style lang="scss" scoped>
.form-container {
  padding: 10px;
  margin-bottom: 10px;
  background-color: white;
  .btn-container {
    display: inline-block;
    float: right;
    vertical-align: middle;
  }
  .el-form {
    display: inline-block;
  }
  .form-line {
    .el-form-item {
      margin: 0;
      margin-right: 20px;
    }
    .el-input {
      width: 240px;
    }
    .el-select {
      width: 240px;
    }
  }
}
.table-container {
  padding: 40px 10px;
  padding-bottom: 60px;
  background-color: white;
  .pagination-container {
    // display: inline-block;
    float: right;
    margin-top: 20px;
    background-color: white;
  }
}
::v-deep .el-form-item__label {
  font-size: 13px;
}
::v-deep .el-button > span {
  font-size: 13px;
}
::v-deep .el-input__inner {
  font-size: 13px;
}
</style>
